<?php

namespace humhub\modules\api\cccstream;

use Yii;
use humhub\modules\content\models\Content;

/**
 * 结合Dashboard和ContentContainer, 获取指定用户的Stream, 需要屏蔽可见性
 */
class ContentContainerStream2 extends Stream
{

    public $contentContainer;

    public function init()
    {
        parent::init();

        //+- Get Content Container by Param
        if ($this->contentContainer->wall_id != "" 
            && $this->contentContainer->className()=='humhub\modules\user\models\User' 
            && $this->contentContainer->id
        ) {
            // User to space memberships
            $spaceMemberships = (new \yii\db\Query())
                    ->select("sm.wall_id")
                    ->from('space_membership')
                    ->leftJoin('space sm', 'sm.id=space_membership.space_id')
                    ->where('sm.created_by='. $this->contentContainer->id .' AND sm.visibility=1 AND space_membership.user_id=' . $this->contentContainer->id . ' AND sm.wall_id IS NOT NULL AND space_membership.show_at_dashboard = 1');
            $union = Yii::$app->db->getQueryBuilder()->build($spaceMemberships)[0];

            // Glue together also with current users wall
            $wallIdsSql = (new \yii\db\Query())
                    ->select('wall_id')
                    ->from('user uw')
                    ->where('uw.id=' . $this->contentContainer->id);
            $union .= " UNION " . Yii::$app->db->getQueryBuilder()->build($wallIdsSql)[0];

            $this->activeQuery->andWhere('wall_entry.wall_id IN (select subselect.wall_id from (' . $union . ') subselect)');

            /**
             * Begin visibility checks regarding the content container
             */
            $this->activeQuery->leftJoin('wall', 'wall_entry.wall_id=wall.id');
            $this->activeQuery->leftJoin(
                    'space_membership', 'wall.object_id=space_membership.space_id AND space_membership.user_id=:userId AND space_membership.status=:status', ['userId' => $this->contentContainer->id, ':status' => \humhub\modules\space\models\Membership::STATUS_MEMBER]
            );

            $condition = ' (wall.object_model=:userModel AND content.visibility=1 AND content.created_by = :userId) OR ';
            // In case of an space entry, we need to join the space membership to verify the user can see private space content
            $condition .= ' (wall.object_model=:spaceModel AND content.visibility = 1 AND space_membership.status = ' . \humhub\modules\space\models\Membership::STATUS_MEMBER . ') OR ';
            $condition .= ' (content.visibility = 1 OR content.visibility IS NULL) ';
            $this->activeQuery->andWhere($condition, [':userId' => $this->contentContainer->id, ':spaceModel' => \humhub\modules\space\models\Space::className(), ':userModel' => \humhub\modules\user\models\User::className()]);

        } else {
            Yii::warning("No wall id for content container " . get_class($this->contentContainer) . " - " . $this->contentContainer->getPrimaryKey() . " set - stopped stream action!");
            // Block further execution
            $this->activeQuery->andWhere("1=2");
        }
    }
}
